package com.example.springboot.control;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.*;

@RestController
@RequestMapping("/sync_list_cnt")
public class sync_list_cnt_ctrl {
    @Autowired
    @Qualifier("primaryJdbcTemplate")
    JdbcTemplate jdbc1;

    @Autowired
    @Qualifier("secondaryJdbcTemplate")
    JdbcTemplate jdbc2;

    @Autowired
    @Qualifier("JdbcTemplate3")
    JdbcTemplate jdbc3;

    public void inserts()
    {
        List<Map<String, Object>> list1=jdbc1.queryForList("select * from sync_list_cnt  order by 1,2");
        List<Map<String, Object>> list2=jdbc2.queryForList("select * from sync_list_cnt  order by 1,2");
        List<Map<String, Object>> list3=jdbc3.queryForList("select * from sync_list_cnt  order by 1,2");
        list1.addAll(list2);
        list1.addAll(list3);
        String sql="insert into sync_list_cnt1 values(?,?,?)";
        jdbc1.batchUpdate(sql, new BatchPreparedStatementSetter()
        {
            public void setValues(PreparedStatement ps, int i)throws SQLException
            {
                Map<String, Object> map1 = list1.get(i);
                ps.setObject(1, map1.get("db"));
                ps.setObject(2, map1.get("tab"));
                ps.setObject(3, map1.get("cnt"));
            }
            public int getBatchSize()
            {
                return list1.size();
            }
        });

    }


    @GetMapping("/findAll")
    public List<Map<String, Object>> findAll(){
        jdbc1.update("truncate table sync_list_cnt1");
        inserts();
        return  jdbc1.queryForList("select * from sync_list_cnt1  order by 1,2");
    }}